#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: KevinQi
@file: jtask.py
@time: 2023/12/26 10:21
@desc: 
"""

from backend.workjob.admin.models.jtask import Task
from backend.workjob.utils.base_requeset import BaseAuthCheckHttpHandler
from backend.workjob.utils.base_response import SuccessResponse, ErrorResponse
from backend.workjob.utils.page_utils import self_pagination


class TaskModelViewSet(BaseAuthCheckHttpHandler):
    """
    Task任务管理
    """

    def get(self, *args, **kwargs):
        if hasattr(self.request, "data"):
            result = Task.filter_list(self.request.data)
        else:
            result = Task.all_list()
            print(result)
        data = self_pagination(self.request, data_list=result)
        return self.write(SuccessResponse(data))

    def post(self, *args, **kwargs):
        """
        task add
        """
        request_data = self.request.data
        if request_data:
            task_obj = Task(**request_data)
            task = Task.add(task_obj, self.request)
            return self.write(SuccessResponse(task))
        return self.write(ErrorResponse(code=500, msg="parameters will not be Null"))

    def put(self, *args, **kwargs):
        id = self.request.data.get("id")
        if id:
            task = Task.first(id=id)
            if task:
                Task.filter_update(filter_args={"id": id}, update_args=self.request.data, request=self.request)
                task = Task.query_first_dict(id=id)
                return self.write(SuccessResponse(data=task))
            else:
                return self.write(ErrorResponse(msg="R u kidding me? 任务不存在!"))

    def delete(self, *args, **kwargs):
        """
        task delete
        """
        id = self.request.data.get("id")
        task = Task.first(id=id)
        if task:
            Task.delete_soft(id=id)
            return self.write(SuccessResponse(data=[]))
        else:
            return self.write(ErrorResponse(msg="任务Task不存在!"))
